<?php

namespace App\Http\Controllers\V1\API;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;

/**
 * 基础控制器(适用于客户端应用)
 *
 * @package App\Http\Controllers\Api
 */
class BaseController extends Controller
{
    // HTTP请求对象
    protected $request;

    // HTTP响应对象
    protected $response;

    /**
     * 构造函数
     */
    public function __construct(Request $request,Response $response)
    {
        $this->request = $request;
        $this->response = $response;
    }

    /**
     * 发送响应数据
     *
     * @param array $data 响应内容
     * @param int $status 状态代码
     * @param array $headers 响应包头
     * @param int $options
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendResponse($data = [], $status = 200, array $headers = [], $options = 0)
    {
        // 日志打印
        $log['status'] = $status;
        $log['header'] = $headers;
        $log['body'] = $data;
        info("RESP:",$log);

        return response()->json($data,$status,$headers,$options);
    }
    
    /**
     * 验证Request数据合法性
     *
     * @param array $data
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validateRequest(array $data, array $rules, array $messages = [], array $customAttributes = [])
    {
        return $this->getValidationFactory()->make($data, $rules, $messages, $customAttributes);
    }
}
